package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.Cat1ModelAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.objenesis.SpringObjenesis;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 二网通标准版包括基础服务、基础服务+miniPCIe
 * 二网通增强版包括所有类型
 * 三网通增强版只有基础服务+miniPCIE版本
 */
@Service
@AttrHandler(Cat1ModelAttrHandler.class)
public class Cat1ModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    private static Map<String, Object> ModelSalePrice = null;//销售价
    private static Map<String, Object> ModelSettlePrice = null;//结算价
    private static Map<String, Object> MaterialCode = null;

    static {
        ModelSalePrice = new HashMap<>();//销售价
        ModelSalePrice.put("110007342", "40"); // (雁飞CAT1模组标准版)
        ModelSalePrice.put("110007343", "40"); // (雁飞Cat1模组LTE三网通增强版)
        ModelSalePrice.put("110007344", "48"); // (雁飞Cat1模组LTE三网通增强版-miniPCIe)
        ModelSalePrice.put("110007345", "55.5"); // (雁飞Cat1模组LTE三网通增强版-GPS)
        ModelSalePrice.put("110007346", "63.5"); // (雁飞Cat1模组LTE三网通增强版-GPS-miniPCIe)
        ModelSalePrice.put("110009600", "40"); // (雁飞Cat1模组增强版)

        ModelSettlePrice = new HashMap<>();//结算价
        ModelSettlePrice.put("110007342", "39"); // (雁飞CAT1模组标准版)
        ModelSettlePrice.put("110007343", "39"); // (雁飞Cat1模组LTE三网通增强版)
        ModelSettlePrice.put("110007344", "47"); // (雁飞Cat1模组LTE三网通增强版-miniPCIe)
        ModelSettlePrice.put("110007345", "54.5"); // (雁飞Cat1模组LTE三网通增强版-GPS)
        ModelSettlePrice.put("110007346", "62.5"); // (雁飞Cat1模组LTE三网通增强版-GPS-miniPCIe)
        ModelSettlePrice.put("110009600", "39"); // (雁飞Cat1模组增强版)

        MaterialCode = new HashMap<>();
        MaterialCode.put("110007342", "7600300752"); // (雁飞CAT1模组标准版)
        MaterialCode.put("110007343", "7600301077"); // (雁飞Cat1模组LTE三网通增强版)
        MaterialCode.put("110007344", "7600301065"); // (雁飞Cat1模组LTE三网通增强版-miniPCIe)
        MaterialCode.put("110007345", "7600301078"); // (雁飞Cat1模组LTE三网通增强版-GPS)
        MaterialCode.put("110007346", "7600301079"); // (雁飞Cat1模组LTE三网通增强版-GPS-miniPCIe)
    }

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String value = attr.getValue();
        // 服务能力
        KiteAttr abilityAttr = inst.getAttr("210004316");
        if (Objects.nonNull(abilityAttr)) {
            // 二网通标准版包括基础服务、基础服务+miniPCIe
            if (KiteStringUtils.equals(value, "110007342")) {
                List<AttrValue> abilityAttrValues = AttrValueCache.get(abilityAttr.getAttrId());
                abilityAttrValues = abilityAttrValues.stream().filter(abilityAttrValue -> KiteStringUtils.equalsAny(abilityAttrValue.getAttrValue(), "110007347", "110007348")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(abilityAttrValues)) {
                    // 默认取第一个
                    AttrValue firstAttr = abilityAttrValues.get(0);
                    abilityAttr.setValue(firstAttr.getAttrValue());
                    abilityAttr.setValueDesc(firstAttr.getAttrValueDesc());
                    abilityAttr.setAttrValues(KiteBeanUtils.copyPropsForList(abilityAttrValues, KiteAttrValue.class));
                    operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), abilityAttr);
                }
            }
            // 三网通增强版只有基础服务+miniPCIE版本
            else if (KiteStringUtils.equalsAny(value, "110007343", "110007344", "110007345", "110007346")) {
                List<AttrValue> abilityAttrValues = AttrValueCache.get(abilityAttr.getAttrId());
                abilityAttrValues = abilityAttrValues.stream().filter(abilityAttrValue -> KiteStringUtils.equalsAny(abilityAttrValue.getAttrValue(), "110007348")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(abilityAttrValues)) {
                    // 默认取第一个
                    AttrValue firstAttr = abilityAttrValues.get(0);
                    abilityAttr.setValue(firstAttr.getAttrValue());
                    abilityAttr.setValueDesc(firstAttr.getAttrValueDesc());
                    abilityAttr.setAttrValues(KiteBeanUtils.copyPropsForList(abilityAttrValues, KiteAttrValue.class));
                    operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), abilityAttr);
                }
            } else {
                List<AttrValue> abilityAttrValues = AttrValueCache.get(abilityAttr.getAttrId());
                if (KiteListUtils.isNotEmpty(abilityAttrValues)) {
                    // 默认取第一个
                    AttrValue firstAttr = abilityAttrValues.get(0);
                    abilityAttr.setValue(firstAttr.getAttrValue());
                    abilityAttr.setValueDesc(firstAttr.getAttrValueDesc());
                    abilityAttr.setAttrValues(KiteBeanUtils.copyPropsForList(abilityAttrValues, KiteAttrValue.class));
                    operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), abilityAttr);
                }
            }
        }

        // 根据选定型号带出价格
        String salePriceValue = KiteMapUtils.getStrValue(ModelSalePrice, value);
        String settlePriceValue = KiteMapUtils.getStrValue(ModelSettlePrice, value);
        String materialCode = KiteMapUtils.getString(MaterialCode, value);
        if (KiteStringUtils.isNotEmpty(salePriceValue)) {
            operateInstAttrService.setAttrValue(inst, "200004107", salePriceValue, data.getSceneInstId(), null);
        }
        if (KiteStringUtils.isNotEmpty(settlePriceValue)) {
            operateInstAttrService.setAttrValue(inst, "200004108", settlePriceValue, data.getSceneInstId(), null);
        }
        String offerInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromGiftOfferInst(data.getCustId(), offerInstId);
        KiteProdInst kiteProdInst = prodInstList.stream().filter(prodInst -> KiteStringUtils.equals(prodInst.getSpecId(), ProdCateUtil.getSubMemberProdId(ProdCateUtil.GIFT_YFCAT1_PROD))).findFirst().orElse(null);
        if (Objects.nonNull(kiteProdInst)) {
            KiteAttr prodFairnessAttr = kiteProdInst.getAttr("200004115");  // 产品公允价
            if (KiteStringUtils.isNotEmpty(settlePriceValue) && Objects.nonNull(prodFairnessAttr)) {
                prodFairnessAttr.setValue(settlePriceValue);
                prodFairnessAttr.setValueDesc(settlePriceValue);
                operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), prodFairnessAttr);
            }
            /*KiteAttr materialCodeAttr = kiteProdInst.getAttr("200004178");  // 物资编码1021版本改为空
            if (KiteStringUtils.isNotEmpty(materialCode) && Objects.nonNull(materialCodeAttr)) {
                materialCodeAttr.setValue(materialCode);
                materialCodeAttr.setValueDesc(materialCode);
                operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), materialCodeAttr);
            }*/
            KiteAttr subModelAttr = kiteProdInst.getAttr("200002772");
            if (KiteStringUtils.isNotEmpty(value) && Objects.nonNull(subModelAttr)) {
                List<AttrValue> subModelAttrValues = AttrValueCache.get(subModelAttr.getAttrId());
                subModelAttrValues = subModelAttrValues.stream().filter(subModAttr -> KiteStringUtils.equals(value, subModAttr.getAttrValue())).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(subModelAttrValues)) {
                    subModelAttr.setValue(value);
                    subModelAttr.setValueDesc(attr.getValueDesc());
                    subModelAttr.setAttrValues(KiteBeanUtils.copyPropsForList(subModelAttrValues, KiteAttrValue.class));
                    operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), subModelAttr);
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
